package com.powernode.money.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.powernode.money.constant.Constants;
import com.powernode.money.mapper.BidInfoMapper;
import com.powernode.money.mapper.IncomeRecordMapper;
import com.powernode.money.mapper.LoanInfoMapper;
import com.powernode.money.model.BidInfo;
import com.powernode.money.model.IncomeRecord;
import com.powernode.money.model.LoanInfo;
import com.powernode.money.service.IncomeRecordService;
import com.powernode.money.vo.IncomeList;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Service(interfaceClass = IncomeRecordService.class, version = "1.0.0", timeout = 15000)
public class IncomeRecordServiceImpl implements IncomeRecordService {
    @Autowired
    private IncomeRecordMapper incomeRecordMapper;
    @Autowired
    private LoanInfoMapper loanInfoMapper;
    @Autowired
    private BidInfoMapper bidInfoMapper;

    @Override
    public List<IncomeList> queryIncomeList(String uid) {
        return incomeRecordMapper.selectIncomeList(uid);
    }

    @Override
    public void generateIncomePlan() {
        //        1.查询产品状态为1已满标的产品 -> 返回List<已满标产品>
        List<LoanInfo> loanInfoList = loanInfoMapper.selectByProductStatus(1);
//        2.循环遍历,获取到每一个产品
        for (LoanInfo loanInfo : loanInfoList) {
//        3.获取当前产品的所有投资记录 -> 返回List<投资记录>
            List<BidInfo> bidInfoList = bidInfoMapper.selectBidInfoByLoanId(loanInfo.getId());
//          4.循环遍历获取到每一个投资记录
            for (BidInfo bidInfo : bidInfoList) {
//        5.将当前的投资记录生成对应的收益计划IncomeRecord
                IncomeRecord incomeRecord = new IncomeRecord();
                incomeRecord.setBidId(bidInfo.getId());
                incomeRecord.setBidMoney(bidInfo.getBidMoney());
                incomeRecord.setIncomeStatus(0);
                incomeRecord.setLoanId(loanInfo.getId());
                incomeRecord.setUid(bidInfo.getUid());
//        a）收益时间(Date) = 产品满标时间(Date) + 产品周期(int 天|月)
                Date incomeDate = null;
                double incomeMoney = 0;
                if (Constants.PRODUCT_TYPE_X.equals(loanInfo.getProductType())) {
                    //新手宝
                    //调用lang3包中的日期工具类计算收益时间
                    incomeDate = DateUtils.addDays(loanInfo.getProductFullTime(), loanInfo.getCycle());
                    //投资金额 * (年利率 / 100 / 365) * 投资周期;
                    incomeMoney = bidInfo.getBidMoney() * (loanInfo.getRate() / 100 / 365) * loanInfo.getCycle();
                } else {
                    //优选或散标
                    incomeDate = DateUtils.addDays(loanInfo.getProductFullTime(), loanInfo.getCycle() * 30);
                    incomeMoney = bidInfo.getBidMoney() * (loanInfo.getRate() / 100 / 365) * loanInfo.getCycle() * 30;
                }
                incomeRecord.setIncomeDate(incomeDate);
                //仅保留2位小数
                incomeRecord.setIncomeMoney(((double) Math.round(incomeMoney * 100)) / 100);
                //保存收益记录
                incomeRecordMapper.insert(incomeRecord);
            }
            //6把生成收益计划的产品状态设置为2
            loanInfo.setProductStatus(2);
            loanInfoMapper.updateByPrimaryKey(loanInfo);
        }
    }
}
